package com.health.circle.service.impl;

import com.health.circle.domain.dto.AnnounceCircleDto;
import com.health.circle.domain.po.PatientCirclePo;
import com.health.circle.domain.po.TreatmentExperiencePo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.health.circle.domain.vo.PatientCircleVo;
import com.health.circle.mapper.PatientCircleMapper;
import com.health.circle.mapper.TreatmentExperienceImageMapper;
import com.health.circle.mapper.TreatmentExperienceMapper;
import com.health.circle.service.PatientCircleService;
import com.health.common.core.exception.ServiceException;
import com.health.common.core.utils.StringUtils;
import com.health.common.security.utils.SecurityUtils;
import com.health.system.api.model.LoginUser;
import jakarta.annotation.Resource;
import com.health.circle.service.IPatientCircleService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 病友圈信息表 服务实现类
 * </p>
 *
 * @author zzs
 * @since 2024-12-16
 * @BelongsProject: dimensionality-manage
 * @BelongsPackage: com.health.circle.service.impl
 * @Author: 许力匀
 * @CreateTime: 2024-12-16  00:48
 * @Description: 病友圈业务层
 * @Version: 1.0
 */
@Service
@Slf4j
public class PatientCircleServiceImpl implements PatientCircleService {

    @Autowired
    private PatientCircleMapper patientCircleMapper;
    /**
     * 查询病友圈信息
     *
     * @param departmentId
     * @return
     */
    @Resource
    private TreatmentExperienceMapper treatmentExperienceMapper;

    @Resource
    private TreatmentExperienceImageMapper treatmentExperienceImagemapper;


    @Transactional
    public void patientCircle(AnnounceCircleDto announceCircleDto) {
        //校验标题
        if (StringUtils.isEmpty(announceCircleDto.getTitle())) {
            throw new ServiceException("标题不允许为空");
        } else if (announceCircleDto.getTitle().length() > 20) {
            throw new ServiceException("标题最多20个字");
        }
        //校验病症详情
        if (StringUtils.isEmpty(announceCircleDto.getDiseaseDetails())) {
            throw new ServiceException("病症详情不允许为空");
        } else if (announceCircleDto.getDiseaseDetails().length() > 300) {
            throw new ServiceException("病症详情不得超过300个字");
        }
        //校验医院名称
        if (announceCircleDto.getHospitalId() == 0) {
            throw new ServiceException("请选择医院");
        }
        //校验开始时间和结束时间
        if (announceCircleDto.getStartDate() == null || announceCircleDto.getEndDate() == null) {
            throw new ServiceException("请选择开始时间和结束时间");
        }
        //校验开始时间不能在结束时间之后
        if (announceCircleDto.getStartDate().getTime() > announceCircleDto.getEndDate().getTime()) {
            throw new ServiceException("开始时间不能在结束时间之后");
        }
        ;
        //校验结束时间不能是当前时间之后的时间
        if (announceCircleDto.getEndDate().getTime() > System.currentTimeMillis()) {
            throw new ServiceException("结束时间不能是当前时间之后的时间");
        }
        ;
        //创建治疗经历表
        TreatmentExperiencePo treatmentExperiencePo = new TreatmentExperiencePo();
        //医院
        treatmentExperiencePo.setHospitalId(announceCircleDto.getHospitalId());
        //开始时间
        treatmentExperiencePo.setStartDate(announceCircleDto.getStartDate());
        //结束时间
        treatmentExperiencePo.setEndDate(announceCircleDto.getEndDate());
        //治疗过程
        treatmentExperiencePo.setTreatmentProcess(announceCircleDto.getTreatmentProcess());
        //添加治疗经历表并且拿到主键自增
        int insert = treatmentExperienceMapper.insert(treatmentExperiencePo);
        //用逗号切割
        String[] split = announceCircleDto.getTreatmentExperienceImageIds().split(",");
        //转list
        List<String> images = Arrays.stream(split).map(String::trim).collect(Collectors.toList());
        //mybatis-plus批量添加数据库
        treatmentExperienceImagemapper.addBatch(treatmentExperiencePo.getId(), images);
        //添加病友圈
        PatientCirclePo patientCirclePo = new PatientCirclePo();
        //标题
        patientCirclePo.setTitle(announceCircleDto.getTitle());
        //科室
        patientCirclePo.setDepartmentId(announceCircleDto.getDepartmentId());
        //病症
        patientCirclePo.setDiseaseId(announceCircleDto.getDiseaseId());
        //病症详情
        patientCirclePo.setDiseaseDetails(announceCircleDto.getDiseaseDetails());
        //是否悬赏
        patientCirclePo.setRewardStatus(announceCircleDto.getRewardStatus());
        //悬赏金额
        if (announceCircleDto.getRewardAmount() == null) {
            patientCirclePo.setRewardAmount(0.0);
        } else {
            patientCirclePo.setRewardAmount(announceCircleDto.getRewardAmount());
        }
        //治疗经历外键
        patientCirclePo.setTreatmentExperienceId(treatmentExperiencePo.getId());
        //发布人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        patientCirclePo.setPublisherId(loginUser.getTuserPo().getId());
        //添加病友圈表
        int insert1 = patientCircleMapper.insert(patientCirclePo);
    }
}
